package org.easy.tool.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;

/**
 * @author Valley
 * Created by Valley on 2023/9/28 11:57
 */
public class ServiceUtil {

    /**
     * 功能：统一service层新增方法
     */
    public static void add(IService<T> service, T entity) {
        service.save(entity);
    }

    /**
     * 功能：统一查询id方法，主键必须为id
     */
    public static T getOne(IService<T> service, String id) {
        return service.getById(id);
    }

    /**
     * 功能：根据条件查询某个值
     */
    public static Object getOne(IService service, SFunction function, String value) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(function, value);
        return service.getOne(wrapper);
    }

    /**
     * 功能：根据某个字段值查询
     */
    public static List<T> list(IService<T> service, SFunction<T, ?> name, String value) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name, value);
        return service.list(wrapper);
    }

    /**
     * 功能：根据关联条件进行查询
     */
    public static void demo() {

    }

}
